package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.resumes.domain.Resumes;
import com.ruoyi.system.domain.Applications;
import com.ruoyi.system.mapper.ApplicationsMapper;
import com.ruoyi.system.service.IApplicationsService;
import com.ruoyi.work.domain.Jobbasicinfo;
import com.ruoyi.work.mapper.JobbasicinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 投递记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-02
 */
@Service
public class ApplicationsServiceImpl implements IApplicationsService
{
    @Autowired
    private ApplicationsMapper applicationsMapper;
    @Autowired
    private JobbasicinfoMapper jobbasicinfoMapper;


    /**
     * 查询投递记录
     *
     * @param id 投递记录主键
     * @return 投递记录
     */
    @Override
    public Applications selectApplicationsById(Long id)
    {

        return applicationsMapper.selectApplicationsById(id);
    }

    @Override
    public Applications selectApplicationByjobid(Long jobid,Long companyid) {
        return applicationsMapper.selectApplicationByjobid(jobid,companyid);
    }

    /**
     * 查询投递记录列表
     *
     * @param applications 投递记录
     * @return 投递记录
     */
    @Override
    public List<Applications> selectApplicationsList(Applications applications) {
        //获取当前用户id
        Long userId=SecurityUtils.getUserId();
        //获取role_id
        List roleId=applicationsMapper.getRoleId(userId);
        //判断是否是应聘者
        if (roleId.contains(100)){
            List<Applications> applications1= applicationsMapper.selectbyuserid(userId);
            List<Applications> applications2= applicationsMapper.selectApplicationsList(applications);
            //两个集合取交集
            Set<Applications> set=new HashSet<>(applications1);
            List<Applications> list=new ArrayList<>();
            for(Applications application:applications2){
                if(set.contains(application)){
                    list.add(application);
                }
            }
            return list;

        }else if(roleId.contains(103)) {  //判断是否是招聘专员、面试官
            List<Applications> applications1= applicationsMapper.selectResumesByRoleId(userId);
            List<Applications> applications2= applicationsMapper.selectApplicationsList(applications);
            //两个集合取交集
            Set<Applications> set=new HashSet<>(applications1);
            List<Applications> list=new ArrayList<>();
            for(Applications application:applications2){
                if(set.contains(application)){
                    list.add(application);
                }
            }
            return list;
        }else if(roleId.contains(101)){
            List<Applications> applications1= applicationsMapper.selectResumesByCompany(userId);
            List<Applications> applications2= applicationsMapper.selectApplicationsList(applications);
            //两个集合取交集
            Set<Applications> set=new HashSet<>(applications1);
            List<Applications> list=new ArrayList<>();
            for(Applications application:applications2){
                if(set.contains(application)){
                    list.add(application);
                }
            }
            return list;
        }
        return applicationsMapper.selectApplicationsList(applications);
    }

    @Override
    public int insertApplications(Applications applications) {
        applications.setCreateTime(DateUtils.getNowDate());
        return applicationsMapper.insertApplications(applications);
    }

    @Override
    public int updateApplications(Applications applications) {
        return applicationsMapper.updateApplications(applications);
    }

    @Override
    public int deleteApplicationsByIds(Long[] ids) {
        return applicationsMapper.deleteApplicationsByIds(ids);
    }

    @Override
    public int deleteApplicationsById(Long id) {
        return applicationsMapper.deleteApplicationsById(id);
    }

    @Override
    public List<Applications> selectbyuserid(Long userid) {

        return applicationsMapper.selectbyuserid(userid);
    }


}
